package com.tanhua.manage.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.common.mapper.UserInfoMapper;
import com.tanhua.common.mapper.UserMapper;
import com.tanhua.common.pojo.PageResult;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.dubbo.server.api.UserLikeApi;
import com.tanhua.dubbo.server.api.UserLocationApi;
import com.tanhua.dubbo.server.vo.UserLocationVo;
import com.tanhua.manage.mapper.DongjieMapper;
import com.tanhua.manage.mapper.UnDongJieMapper;
import com.tanhua.manage.pojo.DongJie;
import com.tanhua.manage.pojo.UnDongJie;
import com.tanhua.manage.pojo.UsersManage;
import com.tanhua.manage.pojo.UsersMaterial;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.TimeUnit;

@Service
public class UsersServiceImpl {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DongjieMapper dongjieMapper;

    @Autowired
    private UnDongJieMapper unDongJieMapper;

    @Reference(version = "1.0.0")
    private UserLikeApi userLikeApi;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Reference(version = "1.0.0")
    private UserLocationApi userLocationApi;

    //查看所有用户
    public PageResult users(Integer pagesize, Integer page) {
        PageResult pageResult = new PageResult();
        List<UsersManage> yonghu = new ArrayList<>();
        Page<UserInfo> objectPage = new Page<>(page, pagesize);
        IPage<UserInfo> userInfoIPage = userInfoMapper.selectPage(objectPage, null);
        List<UserInfo> userInfos = userInfoIPage.getRecords();
        pageResult.setCounts((int) userInfoIPage.getTotal());
        pageResult.setPage(page);
        pageResult.setPagesize(pagesize);
        pageResult.setPages((int) userInfoIPage.getPages());
        for (UserInfo userInfo : userInfos) {
            UsersManage usersManage = new UsersManage();
            usersManage.setId(userInfo.getId());
            usersManage.setAge(userInfo.getAge());
            try {
                Long aLong1 = Long.valueOf(redisTemplate.opsForValue().get("HuoYueover" + userInfo.getId()));
                usersManage.setLastActiveTime(new Date(aLong1));
            } catch (NumberFormatException e) {
                usersManage.setLastActiveTime(new Date());
            }
            usersManage.setLogo(userInfo.getLogo());
            usersManage.setNickname(userInfo.getNickName());
            User user = userMapper.selectById(userInfo.getId());
            usersManage.setMobile(user.getMobile());
            usersManage.setSex(userInfo.getSex().getValue() == 1 ? "男" : "女");
            usersManage.setOccupation(userInfo.getIndustry());
            usersManage.setCity(userInfo.getCity());
            String userstatus = redisTemplate.opsForValue().get("userstatus" + userInfo.getId());
            if (userstatus == null) {
                usersManage.setUserStatus("1");
            } else if (userstatus.equals("2")) {
                usersManage.setUserStatus("2");
            }
            yonghu.add(usersManage);
        }
        pageResult.setItems(yonghu);
        return pageResult;
    }

    //根据id查看用户信息
    public UsersMaterial usersMaterial(Long id) {
        UsersMaterial usersMaterial = new UsersMaterial();
        usersMaterial.setId(id);
        UserInfo userInfo = userInfoMapper.selectById(id);
        User user = userMapper.selectById(id);
        usersMaterial.setNickname(userInfo.getNickName());
        usersMaterial.setMobile(user.getMobile());
        usersMaterial.setSex(userInfo.getSex().getValue() == 1 ? "男" : "女");
        usersMaterial.setAge(userInfo.getAge());
        usersMaterial.setCountBeLiked(userLikeApi.queryFanCount(id));
        usersMaterial.setCountLiked(userLikeApi.queryLikeCount(id));
        usersMaterial.setCountMatching(userLikeApi.queryMutualLikeCount(id));
        usersMaterial.setIncome(userInfo.getIncome());
        usersMaterial.setOccupation(userInfo.getIndustry());
        String userstatus = redisTemplate.opsForValue().get("userstatus" + userInfo.getId());
        if (userstatus == null) {
            usersMaterial.setUserStatus("1");
        } else if (userstatus.equals("2")) {
            usersMaterial.setUserStatus("2");
        }
        usersMaterial.setCreated(userInfo.getCreated());
        usersMaterial.setCity(userInfo.getCity());
        try {
            Long aLong1 = Long.valueOf(redisTemplate.opsForValue().get("HuoYueover" + id));
            usersMaterial.setLastActiveTime(new Date(aLong1));
        } catch (NumberFormatException e) {
            usersMaterial.setLastActiveTime(new Date());
        }
        UserLocationVo userLocationVo = null;
        try {
            userLocationVo = userLocationApi.queryByUserId(id);
            usersMaterial.setLastLoginLocation(userLocationVo.getAddress());
        } catch (Exception e) {
            usersMaterial.setLastLoginLocation("中国北京市昌平区建材城西路16号");
        }
        usersMaterial.setPersonalSignature("渣渣辉");
        usersMaterial.setLogo(userInfo.getLogo());
        usersMaterial.setTags(userInfo.getTags());
        return usersMaterial;
    }

    //冻结
    public Map<String, String> free(DongJie freeze) {
        freeze.setCreated(new Date());
        dongjieMapper.insert(freeze);
        if (freeze.getFreezingTime().equals("1")) {
            redisTemplate.opsForValue().set("userstatus" + freeze.getUserId(), "2", 3, TimeUnit.DAYS);
            if (freeze.getFreezingRange().equals("1")) {
                redisTemplate.opsForValue().set("Freezelogin" + freeze.getUserId(), "1", 3, TimeUnit.DAYS);
            } else if (freeze.getFreezingRange().equals("2")) {
                redisTemplate.opsForValue().set("Freezingspeech" + freeze.getUserId(), "2", 3, TimeUnit.DAYS);
            } else if (freeze.getFreezingRange().equals("3")) {
                redisTemplate.opsForValue().set("Freezedynamic" + freeze.getUserId(), "3", 3, TimeUnit.DAYS);
            }
        } else if (freeze.getFreezingTime().equals("2")) {
            redisTemplate.opsForValue().set("userstatus" + freeze.getUserId(), "2", 7, TimeUnit.DAYS);
            if (freeze.getFreezingRange().equals("1")) {
                redisTemplate.opsForValue().set("Freezelogin" + freeze.getUserId(), "1", 7, TimeUnit.DAYS);
            } else if (freeze.getFreezingRange().equals("2")) {
                redisTemplate.opsForValue().set("Freezingspeech" + freeze.getUserId(), "2", 7, TimeUnit.DAYS);
            } else if (freeze.getFreezingRange().equals("3")) {
                redisTemplate.opsForValue().set("Freezedynamic" + freeze.getUserId(), "3", 7, TimeUnit.DAYS);
            }
        } else if (freeze.getFreezingTime().equals("3")) {
            redisTemplate.opsForValue().set("userstatus" + freeze.getUserId(), "2");
            if (freeze.getFreezingRange().equals("1")) {
                redisTemplate.opsForValue().set("Freezelogin" + freeze.getUserId(), "1");
            } else if (freeze.getFreezingRange().equals("2")) {
                redisTemplate.opsForValue().set("Freezingspeech" + freeze.getUserId(), "2");
            } else if (freeze.getFreezingRange().equals("3")) {
                redisTemplate.opsForValue().set("Freezedynamic" + freeze.getUserId(), "3");
            }
        }
        HashMap<String, String> message = new HashMap<>();
        message.put("message", "操作成功");
        return message;
    }

    //解除冻结
    public Map<String, String> unfreeze(UnDongJie unfreeze) {
        unfreeze.setCreated(new Date());
        unDongJieMapper.insert(unfreeze);
        redisTemplate.delete("userstatus" + unfreeze.getUserId());
        redisTemplate.delete("Freezelogin" + unfreeze.getUserId());
        redisTemplate.delete("Freezingspeech" + unfreeze.getUserId());
        redisTemplate.delete("Freezedynamic" + unfreeze.getUserId());
        HashMap<String, String> message = new HashMap<>();
        message.put("message", "操作成功");
        return message;
    }
}
